package com.fsharemobile.adapter;

import java.util.List;

import com.fsharemobile.R;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class PictureGridViewAdapter extends BaseAdapter {

	String path;
	
	SquaredImageView imageView;
	private final Context context;
	//private final Integer []arrThumbId;
	private final List<String> arrLink;
	
	public PictureGridViewAdapter(Context context, List<String> arr){
		this.context= context;
		this.arrLink=arr;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrLink.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		
		if(convertView==null){
			imageView= new  SquaredImageView(context);
			imageView.setScaleType(ScaleType.CENTER_CROP);
		}else{
			imageView= (SquaredImageView) convertView;
		}
		path= arrLink.get(position);
		new Handler().postDelayed(new  Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.CONNECTIVITY_SERVICE);
				ConnectivityManager con = (ConnectivityManager)
						
						context.getSystemService(
								Context.CONNECTIVITY_SERVICE);
				if (con == null
						|| con.getActiveNetworkInfo() == null
						|| !con.getActiveNetworkInfo()
								.isConnected()) {
					Toast.makeText(context, "no internet", Toast.LENGTH_SHORT).show();
				}else{
					
					Picasso.with(context).load(path).resize(50, 50).placeholder(R.drawable.placeholder).into(imageView);
				}
				
			}
		}, 5000);
		//Picasso.with(context).load(arrThumbId[position]).resize(50, 50).placeholder(R.drawable.placeholder).into(imageView);
		notifyDataSetChanged();
		return imageView;
	}

}
